var timestampToTime = (timestamp) => {
  if (!timestamp) {
    return ''
  }
  var date = new Date(timestamp * 1000);
  var Y = date.getFullYear();
  var M = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1;
  var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
  var h = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
  var m = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
  var s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
  return Y + '-' + M + '-' + D + ' ' + h + ':' + m + ':' + s
}
var timestampToDate = (timestamp) => {
  if (!timestamp) {
    return ''
  }
  var date = new Date(timestamp * 1000);
  var Y = date.getFullYear();
  var M = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1;
  var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
  var h = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
  var m = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
  var s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
  return Y + '-' + M + '-' + D
}
var calculateDiffTime = (start_time) => {
  if (!parseInt(start_time)) return false;
  var endTime = Math.round(new Date() / 1000);
  var timeDiff = 900 - (endTime - start_time)
  if (!(timeDiff > 0)) return false
  var minute = parseInt((timeDiff % 3600) / 60);
  var seconds = parseInt((timeDiff % 3600) % 60);
  minute = minute ? (minute < 10 ? '0' + minute : minute) : '00';
  seconds = seconds ? (seconds < 10 ? '0' + seconds : seconds) : '00';
  return minute + ':' + seconds;
}
//倒计时；
var countDown = function(timeStamp, that) {
  var interval = null,
    countDown, totalSecond = timeStamp - Date.parse(new Date()) / 1000;
  if (totalSecond <= 0) {
    wx.showToast({
      title: '活动已结束',
      icon: 'none',
      duration: 900,
      mask: true,
    })
  }
  
   
  interval = setInterval(function() {
    // 秒数  
    var second = totalSecond;
    // 小时位  
    var hr = Math.floor(second / 3600);
    var hrStr = hr.toString();
    if (hrStr.length == 1) hrStr = '0' + hrStr;

    // 分钟位  
    var min = Math.floor((second - hr * 3600) / 60);
    var minStr = min.toString();
    if (minStr.length == 1) minStr = '0' + minStr;

    // 秒位  
    var sec = second - hr * 3600 - min * 60;
    var secStr = sec.toString();
    if (secStr.length == 1) secStr = '0' + secStr;
    
    that.data.countDownHour = hrStr
    that.data.countDownMinute = minStr
    that.data.countDownSecond = secStr
    totalSecond--;
    
    if (totalSecond <= 0) {
      clearInterval(interval);
      that.data.countDownHour = '00'
      that.data.countDownMinute = '00'
      that.data.countDownSecond = '00'
      setTimeout(() => {
        navigateBack({
          delta: 1
        })
      }, 2000)
    }
  }.bind(that), 1000);
  that.interval = interval;
}

//倒计时2；
var countDown2 = function(timeStamp, that) {
  var totalSecond = timeStamp - Date.parse(new Date()) / 1000;
  
    console.log(totalSecond)
  if (totalSecond <= 0) {
    wx.showToast({
      title: '活动已结束',
      icon: 'none',
      duration: 900,
      mask: true,
    })
  }

  var interval = setInterval(function() {
    // 秒数  
    var second = totalSecond;
    
    // // 天数位  
    var day = Math.floor(second / 3600 / 24);
    var dayStr = day.toString();
    if (dayStr.length == 1) dayStr = '0' + dayStr;
    // 小时位  
    var hr = Math.floor((second - day * 3600 * 24) / (60 * 60));
    var hrStr = hr.toString();
    if (hrStr.length == 1) hrStr = '0' + hrStr;

    // 分钟位  
    // var min = Math.floor((second - hr * 3600) / 60);
    var min = Math.floor((second - day * 3600 * 24 - hr * 3600) / 60);
    var minStr = min.toString();
    if (minStr.length == 1) minStr = '0' + minStr;

    // 秒位  
    // var sec = second - hrNew * 3600 - min * 60;
    var sec = Math.floor(second - day * 3600 * 24 - hr * 3600 - min * 60);
    var secStr = sec.toString();
    if (secStr.length == 1) {
      secStr = '0' + secStr;
    }
    
     that.$set(that.data,'countDownDay',dayStr);
     that.$set(that.data,'countDownHour',hrStr);
     that.$set(that.data,'countDownMinute',minStr);
     that.$set(that.data,'countDownSecond',secStr);
     totalSecond--;
    
    if (totalSecond <= 0) {
      clearInterval(interval);
      that.$set(that.data,'countDownDay','00');
      that.$set(that.data,'countDownHour','00');
      that.$set(that.data,'countDownMinute','00');
      that.$set(that.data,'countDownSecond','00');
    }
  }.bind(that), 1000);
  that.interval = interval;

}

// 小数相加
var accAdd = (data1, data2) => {
  var r1, r2, m, c;
  try {
    r1 = data1.toString().split(".")[1].length;
  } catch (e) {
    r1 = 0;
  }
  try {
    r2 = data2.toString().split(".")[1].length;
  } catch (e) {
    r2 = 0;
  }
  c = Math.abs(r1 - r2);
  m = Math.pow(10, Math.max(r1, r2));
  if (c > 0) {
    var cm = Math.pow(10, c);
    if (r1 > r2) {
      data1 = Number(data1.toString().replace(".", ""));
      data2 = Number(data2.toString().replace(".", "")) * cm;
    } else {
      data1 = Number(data1.toString().replace(".", "")) * cm;
      data2 = Number(data2.toString().replace(".", ""));
    }
  } else {
    data1 = Number(data1.toString().replace(".", ""));
    data2 = Number(data2.toString().replace(".", ""));
  }
  return (data1 + data2) / m;
}
// 小数相减
var numSub = (arg1, arg2) => {
  var r1, r2, m, n;
  try {
    r1 = arg1.toString().split(".")[1].length
  } catch (e) {
    r1 = 0
  }
  try {
    r2 = arg2.toString().split(".")[1].length
  } catch (e) {
    r2 = 0
  }
  m = Math.pow(10, Math.max(r1, r2));
  //last modify by deeka
  //动态控制精度长度
  n = (r1 >= r2) ? r1 : r2;
  return ((arg1 * m - arg2 * m) / m).toFixed(2);
};
// 小数相乘
var accMulti = (data1, data2) => {
  var baseData = 0;
  try {
    baseData += data1.toString().split(".")[1].length;
  } catch (e) {}
  try {
    baseData += data2.toString().split(".")[1].length;
  } catch (e) {}
  return Number(data1.toString().replace(".", "")) * Number(data2.toString().replace(".", "")) / Math.pow(10,
    baseData);
}
// 小数相除
var division = (arg1, arg2) => {
  var t1 = 0,
    t2 = 0,
    r1, r2;
  try {
    t1 = new String(arg1).split(".")[1].length;
  } catch (e) {}
  try {
    t2 = arg2.toString().split(".")[1].length;
  } catch (e) {}
  r1 = Number(arg1.toString().replace(".", ""));
  r2 = Number(arg2.toString().replace(".", ""));
  var newData = accMulti((r1 / r2), Math.pow(10, t2 - t1));
  return newData;
}
var navigateTo = (OBJECT) => {
  uni.navigateTo(OBJECT)
}
var redirectTo = (OBJECT) => {
  uni.redirectTo(OBJECT)
}
var reLaunch = (OBJECT) => {
  uni.reLaunch(OBJECT)
}
var switchTab = (OBJECT) => {
  uni.switchTab(OBJECT)
}
var navigateBack = (OBJECT) => {
  uni.navigateBack(OBJECT)
}
var setStorageSync = (KEY, DATA) => {
  uni.setStorageSync(KEY, DATA)
}
var getStorageSync = (KEY) => {
  return uni.getStorageSync(KEY)
}
var removeStorageSync = (KEY) => {
  uni.removeStorageSync(KEY)
}
var clearStorageSync = () => {
  uni.clearStorageSync()
}
var previewImage = (OBJECT) => {
  uni.previewImage(OBJECT)
}
var chooseImage = (OBJECT) => {
  uni.chooseImage(OBJECT)
}
var getImageInfo = (OBJECT) => {
  uni.getImageInfo(OBJECT)
}
var saveImageToPhotosAlbum = (OBJECT) => {
  uni.saveImageToPhotosAlbum(OBJECT)
}
var makePhoneCall = (OBJECT) => {
  uni.makePhoneCall(OBJECT)
}
var setNavigationBarTitle = (OBJECT) => {
  uni.setNavigationBarTitle(OBJECT)
}
var getSystemInfo = (OBJECT) => {
  uni.getSystemInfo(OBJECT)
}
var createSelectorQuery = () => {
  return uni.createSelectorQuery()
}
var openLocation = (OBJECT) => {
  uni.openLocation(OBJECT)
}
var getLocation = (OBJECT) => {
  uni.getLocation(OBJECT)
}
var openSetting = (OBJECT) => {
  uni.openSetting(OBJECT)
}
var pageScrollTo = (OBJECT) => {
  uni.pageScrollTo(OBJECT)
}
var tool_login = (OBJECT) => {
  uni.login(OBJECT)
}
var setClipboardData = (OBJECT) => {
  uni.setClipboardData(OBJECT)
}
var createAnimation = (OBJECT) => {
  return uni.createAnimation(OBJECT)
}
var requestSubscribeMessage = (OBJECT) => {
  uni.requestSubscribeMessage(OBJECT)
}
var requestPayment = (OBJECT) => {
  uni.requestPayment(OBJECT)
}
var chooseAddress = (OBJECT) => {
  uni.chooseAddress(OBJECT)
}
var uploadFile = (OBJECT) => {
  uni.uploadFile(OBJECT)
}
var showLoading = (OBJECT) => {
  uni.showLoading(OBJECT)
}
var hideLoading = () => {
  uni.hideLoading()
}
var showActionSheet = (OBJECT) => {
  uni.showActionSheet(OBJECT)
}
var showModal = (OBJECT) => {
  uni.showModal(OBJECT)
}
var showToast = (OBJECT) => {
  uni.showToast(OBJECT)
}
var getUserProfile = (OBJECT) => {
  uni.getUserProfile(OBJECT)
}
export {
  timestampToTime,
  timestampToDate,
  calculateDiffTime,
  countDown,
  countDown2,
  accAdd,
  numSub,
  accMulti,
  division,
  navigateTo,
  redirectTo,
  reLaunch,
  switchTab,
  navigateBack,
  setStorageSync,
  getStorageSync,
  removeStorageSync,
  clearStorageSync,
  previewImage,
  chooseImage,
  getImageInfo,
  saveImageToPhotosAlbum,
  makePhoneCall,
  setNavigationBarTitle,
  getSystemInfo,
  createSelectorQuery,
  openLocation,
  getLocation,
  openSetting,
  pageScrollTo,
  tool_login,
  setClipboardData,
  createAnimation,
  requestSubscribeMessage,
  requestPayment,
  chooseAddress,
  uploadFile,
  showLoading,
  hideLoading,
  showActionSheet,
  showModal,
  showToast,
  getUserProfile
};
